package com.rainner.admin.action;



import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.rainner.admin.action.base.JsonAction;
import com.rainner.admin.defined.ImageDefine;

public class UploadAction extends JsonAction {
	private static final long serialVersionUID = 1L;

	private File image; // 上传的文件
	private String imageFileName; // 文件名称
	private String imageContentType; // 文件类型


	@Override
	public String execute() throws Exception {
		String realPath = ServletActionContext.getServletContext().getRealPath("/").split("library-mannager")[0];
		realPath = realPath + "library-res\\image-cache";
//		File file = new File(realPath);
//		if(!file.exists()){
//			file.mkdir();
//		}
		/*上传的时候遇到中文问题,是因为要在服务器配置
		 * <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URLEncoding="UTF-8"/>
		 * */
		if(image != null){
			File saveFile = new File(new File(realPath), imageFileName);
			if(!saveFile.getParentFile().exists()){
				saveFile.getParentFile().createNewFile();
			}
			FileUtils.copyFile(image, saveFile);
			ImageDefine.imageDir = "/library-res/image-cache/" + imageFileName;
		}
		return null;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

}
